﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyLibary.BIZ;
using System.Data;

namespace MyLibary.DAL
{
    class DAL_ChiTietBan
    {
       private static DAL_Connection connect = new DAL_Connection();
        #region Metholds
       internal static List<BIZ_ChiTietBan> getAll(string MaHDBan)
       {
           DataTable tb = connect.ExecuteSelect("SELECT * FROM ChiTietBan WHERE MaHDBan='"+MaHDBan+"'");
           List<BIZ_ChiTietBan> result = new List<BIZ_ChiTietBan>();
           for (int i = 0; i < tb.Rows.Count; i++)
               result.Add(new BIZ_ChiTietBan(tb.Rows[i][0].ToString(), tb.Rows[i][1].ToString(), Convert.ToDecimal(tb.Rows[i][2].ToString()), Convert.ToInt32(tb.Rows[i][3].ToString())));
           return result;
       }
       internal static BIZ_ChiTietBan getByID(string MaHDBan, string MaSanPham)
       {
           DataTable tb = connect.ExecuteSelect(String.Format("SELECT * FROM ChiTietBan WHERE MaHDBan='{0}' AND MaSanPham='{1}'", MaHDBan, MaSanPham));
           if (tb.Rows.Count < 1)
               return null;
           return new BIZ_ChiTietBan(tb.Rows[0][0].ToString(), tb.Rows[0][1].ToString(), Convert.ToDecimal(tb.Rows[0][2].ToString()), Convert.ToInt32(tb.Rows[0][3].ToString()));
       }
       internal static decimal getTongTien(String MaHDBan)
       {
           DataTable tb = connect.ExecuteSelect("SELECT SUM(GiaBan*SoLuong) FROM ChiTietBan WHERE MaHDBan='" + MaHDBan + "'");
           decimal result;
           try
           {
               result=Convert.ToDecimal(tb.Rows[0][0].ToString());
           }
           catch { result = 0; }
           return result;
       }
       internal static bool Insert(BIZ_ChiTietBan chiTietBan)
       {
           BIZ_Kho kho = DAL_Kho.getBySanPham(chiTietBan.MaSanPham, chiTietBan.HDBan.NgayLap);
           if (kho == null || kho.SoLuongTon < chiTietBan.SoLuong)
               return false;
           bool success= connect.ExecuteUpdate(String.Format("INSERT INTO ChiTietBan VALUES('{0}','{1}','{2}','{3}')", chiTietBan.MaHDBan, chiTietBan.MaSanPham, chiTietBan.DonGia, chiTietBan.SoLuong));
           if (success)
           {
               kho.SoLuongTon -= chiTietBan.SoLuong;
               kho.Update();
           }
           return success;
       }
       public static bool Update(BIZ_ChiTietBan chiTietBan)
       {
           BIZ_ChiTietBan old = DAL_ChiTietBan.getByID(chiTietBan.MaHDBan, chiTietBan.MaSanPham);
           if (old == null)
               return false;
           int temp = old.SoLuong - chiTietBan.SoLuong;
           BIZ_Kho kho=DAL_Kho.getBySanPham(chiTietBan.MaSanPham,chiTietBan.HDBan.NgayLap);
           int currentStore =kho.SoLuongTon;
           int real = currentStore + temp;
           bool success= connect.ExecuteUpdate(string.Format("UPDATE ChiTietBan SET GiaBan={0},SoLuong={1} WHERE MaHDBan='{2}' AND MaSanPham='{3}'", chiTietBan.DonGia, chiTietBan.SoLuong, chiTietBan.MaHDBan, chiTietBan.MaSanPham));
           if (success)
           {
               kho.SoLuongTon = real;
               kho.Update();
           }
           return success;
       }
       public static bool Delete(BIZ_ChiTietBan chiTietBan)
       {
           BIZ_ChiTietBan old = DAL_ChiTietBan.getByID(chiTietBan.MaHDBan, chiTietBan.MaSanPham);
           if (old == null)
               return false;
           bool success= connect.ExecuteUpdate(String.Format("DELETE ChiTietBan WHERE MaHDBan='{0}' AND MaSanPham='{1}'", chiTietBan.MaHDBan, chiTietBan.MaSanPham));
           if (success)
           {
               BIZ_Kho kho = DAL_Kho.getBySanPham(chiTietBan.MaSanPham, chiTietBan.HDBan.NgayLap);
               if (kho != null)
               {
                   kho.SoLuongTon += old.SoLuong;
                   kho.Update();
               }
           }
           return success;
       }
        #endregion
    }
}
